﻿using EFCore7.Migrations;
using MySql.Data.EntityFramework;
using System.Data.Entity;

namespace EFCore7
{
    /// <summary>
    /// 数据库初始化操作类
    /// </summary>
    public class DatabaseInitializer
    {
        /// <summary>
        /// 设置数据库初始化，策略为自动迁移到最新版本
        /// </summary>
        public static void Initialize()
        {
            DbConfiguration.SetConfiguration(new MySqlEFConfiguration());

            MyDbContext context = new MyDbContext();
            IDatabaseInitializer<MyDbContext> initializer;
            if (!context.Database.Exists())
            {
                initializer = new CreateDatabaseIfNotExistsWithSeed();
            }
            else
            {
                initializer = new MigrateDatabaseToLatestVersion<MyDbContext, MigrationsConfiguration>();
            }
            Database.SetInitializer(initializer);
            context.Dispose();
        }
    }
}